/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package p1;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Set;

/**
 *
 * @author Derek Ward
 * 
 * This is an interpretation of the entire graph
 * containing a map of nodes, referenced by their 
 * node labels
 * 
 */
public class Graph 
{
    private     HashMap<String, Node>       m_nodeList;
    private     ArrayList<Edge>             m_edgeList;  // even though this isn't used
                                                         // the method d.createEdgeArrayList(m_nodeList)
                                                         // is very important!!!
    
    public Graph(DataFile d) throws IOException, InvalidEdgeWeightException
    {
        if ( d == null )
            throw new NullPointerException("Graph::Graph(DataFile) was passed a null argument [d]!");
        
        d.readDataFile();
        m_nodeList = d.createNodeHashMap();
        m_edgeList = d.createEdgeArrayList(m_nodeList);
    }
    
    public void printGraph()
    {
        Set<String> s = m_nodeList.keySet();
        Node v;
        
        for(String st : s)
        {
            v = m_nodeList.get(st);
            System.out.println("Node: <" + v.getLabel() + "|" + v.m_min + ">");
            for(Edge e : v.m_connectedEdges)
            {
                System.out.println("\tEdge: [" + e.getSourceNode().getLabel() + 
                                    "]-(" + e.getWeight() + ")-[" + 
                                    e.getDestinationNode().getLabel() + "]");
            }
        }
    }
    
    public boolean isValidNode(String s)
    {
        if ( s == null )
            throw new NullPointerException("Graph::istValidNode(String) was passed a null argument [s]!");
        
        return m_nodeList.containsKey(s);
    }
    
    public Node getNode(String s)
    {
        if ( s == null )
            throw new NullPointerException("Graph::istValidNode(String) was passed a null argument [s]!");
        
        return m_nodeList.get(s);
    }
    
    public Collection<Node> getNodeList()
    {
        return m_nodeList.values();
    }
    
    public void resetNodeValues()
    {
        Set<String>     k = m_nodeList.keySet();
        
        for ( String s : k )
        {
            m_nodeList.get(s).reset();
        }
    }
}
